package chapter5.v2;

import basic.lessons.TextIO;

public class PlayBlackJack {
	private static int bankMoney=100;
	



	public static void main(String[] args) {
		int inputValue = 1;
		while (inputValue != 0) {
			TextIO.putln("Let's play BlackJack!");
			TextIO.putln("Type '0' to quit program");
			inputValue = TextIO.getlnInt();
			int s = betMoney();
			boolean b = playOneGame ();
			if (b) bankMoney+=s;
			else bankMoney-=s;
			
			 TextIO.putln();
		     TextIO.putln("Your current Bank is "+bankMoney);
			
		}
		

	}
	/**
	 This subroutine plays one game and returns TRUE if user wins and
	 FALSE if dealer wins
	 */


	public static boolean playOneGame (){
		/**
		 Initializing dealer's and user's Hands,
		 creating a Deck and shuffling it. Create a Card
		 to add to a Hand
		 */
		
		BlackjackHand userHand = new BlackjackHand(); 
		BlackjackHand dealerHand = new BlackjackHand();
		Deck d = new Deck();
		d.shuffle();
		int dealtCards=1; // counting dealt cards
		Card c = new Card();
		
		/**
		 Dealing for first 2 cards to the both Hands
		 */
		
		while (dealtCards <=2){
			c = d.dealCard();
			userHand.addCard(c);
			dealtCards++;		
		}
		/**
		 Display user's Hand and compute its value
		 */
		
		userHand.displayHand(false);
		userHand.displayBlackjackValue();
		
		dealtCards=1;
		while (dealtCards <=2){
			c = d.dealCard();
			dealerHand.addCard(c);
			dealtCards++;		
		}
		/**
		 Display dealer's Hand and compute its value
		 */
		
		dealerHand.displayHand(true);
		dealerHand.displayBlackjackValue();
		
		
		/**
		 If dealer has 21, the game is lost, otherwise continue
		 */
		if (dealerHand.getBlackjackValue()==21) return false;
		else if (userHand.getBlackjackValue()==21) return true;
		
		boolean answer = hitOrStand();
		
		while (answer){
			c = d.dealCard();
			userHand.addCard(c);
			userHand.displayHand(false);
			userHand.displayBlackjackValue();
			
			if (userHand.getBlackjackValue() > 21) {
				TextIO.putln("Your Hand is bigger than 21, You LOSE the game!");
				return false;
			}
			else answer=hitOrStand();
		}
		
		/** Now it's dealer's turn.
		 * Dealer hits cards until his Hand is > 16
		 */
		
		while (dealerHand.getBlackjackValue() <= 16){
			c = d.dealCard();
			dealerHand.addCard(c);
			dealerHand.displayHand(true);
			dealerHand.displayBlackjackValue();
			
			if (dealerHand.getBlackjackValue() > 21) {
				TextIO.putln("Dealer's Hand is bigger than 21, You WIN the game!");
				return true;
			}
		}

		/** Let's compare Hands and find the winner
		 * Dealer hits cards until his Hand is > 16
		 */
		
		if (userHand.getBlackjackValue() <= dealerHand.getBlackjackValue()){
			TextIO.putln("Your Hand is less than Dealer's, You LOSE the game!");
			return false;}
		else {
			TextIO.putln("Your Hand is bigger than Dealer's, You WIN the game!");
			return true;
		}
		
		
		
	}// end of playOneGame method
			
		
		/** Asks player to hit or stand with current Hand
		 * returns TRUE for Hit and FALSE for Stand
		 */
		
		public static boolean hitOrStand(){
			 String choice;
			 TextIO.putln();
		     TextIO.putln("Hit? (press h, or H) or ");
		     TextIO.putln("Stand? (press s, or S) ");
		     choice = TextIO.getlnString().toLowerCase();
		     

		     switch (choice) {
		     case "h": 
		    	 return true ;
		     case "s": 
		    	 return false ;
		    	 default:
		    return true;
		     }

		}// end of hitOrStand method
		
		
		public static int betMoney(){
			 TextIO.putln();
		     TextIO.putln("Type the sum You'd like to bet and press Enter key");
		     int sumMoney = TextIO.getlnInt();
		     return sumMoney;			
		}
	} // end of PlayBlackJack Class
	
	


